﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManagement
{
    #region Using
    using DevComponents.DotNetBar;
    using AppManagement.Helpers;
    using AppManagement.Repositories;
    using AppManagement.UserManagers;
    using AppManagement.MainManagers;
    using AppManagement.Reports; 
    #endregion

    public partial class MainForm : Office2007Form 
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void menQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
        }

        private void menDangXuat_Click(object sender, EventArgs e)
        {
            GlobalVariables.g_CurrentUser = null;

            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        public void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVariables.g_CurrentUser = NhanVienRepository.SelectByUserName(username);

            if (GlobalVariables.g_CurrentUser == null)
            {
                success = false;
                infoText = "Người dùng này chưa có trong hệ thống";
            }
            else
            {
                GlobalVariables.g_CurrentUser = NhanVienRepository.SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVariables.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Bạn đã nhập sai mật khẩu";
                }
                else
                {
                    infoText = "Chào bạn  " + GlobalVariables.g_CurrentUser.TenDangNhap + "  vào hệ thống !";
                }
            }

            if (!success)
            {
                MessageBoxEx.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmDangNhap frm = new FrmDangNhap();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                // Get current user
                GlobalVariables.g_CurrentUser = NhanVienRepository.SelectByUserName(username);
                MessageBoxEx.Show(infoText);
            }
        }

        private void menDangNhap_Click(object sender, EventArgs e)
        {
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string ErrorText = "";

            if (!System.IO.File.Exists(GlobalVariables.g_strPathConfig))
            {
                FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                frm.ShowDialog();
            }
            else
            {
                XMLProvider _provider = new XMLProvider();
                // Load config file to get infos that used to connect to DB
                _provider.GetConnectionInfo(ref ErrorText);
                if (ErrorText != "")
                {
                    MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    // Re-create connection infos
                    FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                    frm.ShowDialog();
                }
                else
                {
                    // Get connection string successfull, then open connection
                    var lstNguoiDung = NhanVienRepository.SelectAll();
                    if (lstNguoiDung.Count > 0)
                    {
                        FrmDangNhap frm = new FrmDangNhap();
                        frm.Handler += ValidateLogin;
                        frm.ShowDialog();
                    }
                    else
                    {
                        // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                        menDangXuat.Enabled = false;
                        menDoiMatKhau.Enabled = false;
                    }
                }
            }
        }

        private void menDoiMatKhau_Click(object sender, EventArgs e)
        {
            FrmDoiMatKhau frm = new FrmDoiMatKhau();
            frm.ShowDialog();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menDiaDiem_Click(object sender, EventArgs e)
        {
            FrmQuanLyDiaDiem frm = new FrmQuanLyDiaDiem();
            frm.ShowDialog();
        }

        private void menQuanLyXe_Click(object sender, EventArgs e)
        {
            FrmQuanLyXe frm = new FrmQuanLyXe();
            frm.ShowDialog();
        }

        private void menQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            FrmQuanLyNhanVien frm = new FrmQuanLyNhanVien();
            frm.ShowDialog();
        }

        private void menQuanLyKhachHang_Click(object sender, EventArgs e)
        {
            FrmQuanLyKhachHang frm = new FrmQuanLyKhachHang();
            frm.ShowDialog();
        }

        private void menTuyenXe_Click(object sender, EventArgs e)
        {
            FrmQuanLyTuyenXe frm = new FrmQuanLyTuyenXe();
            frm.ShowDialog();
        }

        private void menThongTinLienHe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }

        private void btnDanhSachTuyenXe_Click(object sender, EventArgs e)
        {
            FrmQuanLyTuyenXe frm = new FrmQuanLyTuyenXe();
            frm.ShowDialog();
        }

        private void menTimNhanVien_Click(object sender, EventArgs e)
        {
            FrmTimKiemNhanVien frm = new FrmTimKiemNhanVien();
            frm.ShowDialog();
        }

        private void menTimChuyenXe_Click(object sender, EventArgs e)
        {
            FrmTimKiemChuyenXe frm = new FrmTimKiemChuyenXe();
            frm.ShowDialog();
        }

        private void btnLichHoatDong_Click(object sender, EventArgs e)
        {
            FrmLichHoatDong frm = new FrmLichHoatDong();
            frm.ShowDialog();
        }

        private void btnDatVeXe_Click(object sender, EventArgs e)
        {
            FrmDatVeXe frm = new FrmDatVeXe();
            frm.ShowDialog();
        }

        private void menThongKeDoanhThu_Click(object sender, EventArgs e)
        {
            FrmThongKeDoanhThu frm = new FrmThongKeDoanhThu();
            frm.ShowDialog();
        }

        private void btnDoanhThu_Click(object sender, EventArgs e)
        {
            FrmThongKeDoanhThu frm = new FrmThongKeDoanhThu();
            frm.ShowDialog();
        }

        private void menInVeXe_Click(object sender, EventArgs e)
        {

        }
    }
}
